import Vue from "vue";
import axios from "axios";
import ElementUI from 'element-ui';
import router from '@/router'
const http = axios.create({
    timeout: 1000 * 30,
    withCredentials: true,
    headers: {
        'Content-Type': 'application/json; charset=utf-8'
    }
})
/**
 * 请求拦截
 */
http.interceptors.request.use(config => {
    config.headers['token'] = Vue.cookie.get('token') // 请求头带上token
    config.headers['projectCode']=Vue.cookie.get('projectCode')
    return config
}, error => {
    return Promise.reject(error)
})
/**
 * 响应拦截
 */
http.interceptors.response.use(response => {
    if (response.data && response.data.code === 99) { //token失效

        ElementUI.Message({
            message: response.data.msg,
            type: 'warning'
        });
        router.push({ name: 'login' })
    }
    if (response.data && response.data.code === 88) { //  token锁定

        ElementUI.Message({
            message: response.data.msg,
            type: 'warning'
        });
        router.push({ name: 'login' })
    }
    return response
}, error => {
    return Promise.reject(error)
})
export default http;
